// ImgPropDialog.cpp : implementation file
//

#include "stdafx.h"
#include <pgl/mfc/PGLMFCInclude.h>
#include "ImgPropDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CImgPropDialog dialog


CImgPropDialog::CImgPropDialog(CWnd* pParent,HDC hDC, const CRect& rect,CPGLRegionPtr pRegion, CPGLArchive::EFileFormat eFileFormat, LPCTSTR szFileName)
	: CDialog(CImgPropDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CImgPropDialog)
	m_bCheckRatio = FALSE;
	m_uHeight = 0;
	m_uWidth = 0;
	//}}AFX_DATA_INIT

	m_pRegion = pRegion;
	m_eFileFormat = eFileFormat;
	m_sFileName = szFileName;
	m_rect = rect;
	m_hDC = hDC;
}


void CImgPropDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CImgPropDialog)
	DDX_Check(pDX, IDC_CHECK_RATIO, m_bCheckRatio);
	DDX_Text(pDX, IDC_EDIT_HEIGHT, m_uHeight);
	DDX_Text(pDX, IDC_EDIT_WIDTH, m_uWidth);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CImgPropDialog, CDialog)
	//{{AFX_MSG_MAP(CImgPropDialog)
	ON_EN_CHANGE(IDC_EDIT_HEIGHT, OnChangeEditHeight)
	ON_EN_CHANGE(IDC_EDIT_WIDTH, OnChangeEditWidth)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImgPropDialog message handlers


void CImgPropDialog::OnOK() 
{
	UpdateData(TRUE);

	// OK has been pressed...
	CPGLArchive ar(m_pRegion);

	if (ar.ExportToFile(m_eFileFormat, m_sFileName, CPGLPointU(m_uWidth, m_uHeight),m_hDC))
	{
		AfxMessageBox(_T("File Succesfully exported"));
	}
	else
	{
		AfxMessageBox(_T("Could not export file."));
	}
	
	CDialog::OnOK();
}

BOOL CImgPropDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	if (!m_pRegion)
		return FALSE;

	m_uWidth = m_rect.Width();
	m_uHeight=m_rect.Height();
	m_bCheckRatio = true;

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CImgPropDialog::OnChangeEditHeight() 
{
	UpdateData(TRUE);

	m_uHeight = __max(1,m_uHeight);
	if (m_bCheckRatio)
	{
		m_uWidth = __max(1,(UINT)floor(m_uHeight/(double)m_rect.Height()*m_rect.Width()));
	}

	UpdateData(FALSE);	
}

void CImgPropDialog::OnChangeEditWidth() 
{
	UpdateData(TRUE);
	m_uWidth = __max(1,m_uWidth);
	if (m_bCheckRatio)
	{
		m_uHeight = __max(1,(UINT)floor(m_uWidth/(double)m_rect.Width()*m_rect.Height()));
	}
	UpdateData(FALSE);		
}
